Why Python?

  • Python is a modern, general-purpose, object-oriented, high-level programming language.
  • Clean and simple language: Easy-to-read and intuitive code, easy-to-learn minimalistic syntax, maintainability scales well with size of projects.
  • Expressive language: Fewer lines of code, fewer bugs, easier to maintain.

Technical details:

  • Dynamically typed: No need to define the type of variables, function arguments or return types.
  • Garbage collection: No need to explicitly allocate and deallocate memory for variables and data arrays.
  • Interpreted: No need to compile the code. The Python interpreter reads and executes the python code directly.
  • Cross platform

Python pro's and con's

Advantages:

  • The main advantage is ease of programming, minimizing the time required to develop, debug and maintain the code.
  • Well designed language that encourages many good programming practices:
    • Modular and object-oriented programming, good system for packaging and re-use of code.
    • Documentation tightly integrated with the code.
  • A large standard library, and a large collection of add-on packages.
  • Interface to many other languages and libraries (no lock-in)

Disadvantages:

  • Since Python is an interpreted and dynamically typed programming language, the execution of python code can be slow compared to compiled statically typed programming languages, such as C and Fortran.
  • Somewhat decentralized, with different environment, packages and documentation spread out at different places. Can make it harder to get started.
  • So far there is no equivalent to the Matlab/Simulink combo

What makes python suitable for scientific computing?

  • Python has a strong position in scientific computing:

    • Large community of users, easy to find help and documentation.
    • Specialized commercial support available.
  • Extensive ecosystem of scientific libraries and environments

  • Great performance due to close integration with time-tested and highly optimized codes written in C and Fortran:

    • blas, altas blas, lapack, arpack, Intel MKL, ...
  • Good support for

    • Parallel processing with processes and threads
    • Interprocess communication (MPI)
    • GPU computing (OpenCL and CUDA)
  • Readily available and suitable for use on high-performance computing clusters.

  • Open source:

    • no license costs and license server issues.
    • open means work can be shared more easily (reproducable science and all that)

Course Format

  • Guided self learning
  • Once every month (this course is a small project with limited resources)
  • Use the wiki (reading and updating), share experience on the forum on Campusnet
  • Establishing a DTU Wind Energy user comminity
  • Feedback is welcom, are there any special requirements?

Proposed Future Classes

  • General Python
  • Core Python modules for scientific use
  • Developing best practices and tools: documentation generation, code comments, unit testing
  • Debugging, profiling code
  • Optimizing code, wrapping C/Fortran
  • Parallel and concurrency methods in python
  • Multi-platform GUI programming in python
  • Virtual environments
  • Data storage and interfaces: from pickle to database

Agenda for this class

  • General introduction: the scientific Python ecosystem
  • What about Python 2 and 3?
  • Three hands on examples on how we use Python
  • Questions Code Academy exercises
  • More Code Academy Exercises

The Python Scientific Ecosystem

  • Python is managed by the independent Python Software Foundation (PSF)
  • The scientific comunity is represented by the NumFOCUS foundation
  • Conferences:
    • EuroScipy 2013: 5 days (tutorials, presentations/posters, sprints), 100 participants
    • Scipy 2013 (in the US/Canada): 5 days (tutorials, presentations/posters, sprints), 340 participants
    • pyvideo: video database of Python talks
    • PyCon: Overview of general Python related conferences
  • Commerical support and training with a strong focus on the scientific context:
  • Used and supported by many major IT companies globaly
  • Scripting bindings for engineering packages such as Abaqus, Salome-Meca/Code_Aster, OpenFOAM
  • SciPy Central: aims to be an equivalent to the MATLAB Central repository
  • Planet SciPy: aggregator of relevant blogs by developers and companies

The SciPy Stack: Core Packages

  • The SciPy-core-stack
    • NumPy: fast multi-dimensional array object. Basis for almost all scientific modules
    • SciPy: fundamental library for scientific computing
    • Matplolib: publication quality plotting library
    • pandas: data structures and data analysis tools, think time series, inspired by R
    • IPython: interactive Python shell, notebook
    • SymPy: symbolic mathematics, computer algebra system (CAS), think Maple, Mathematica

Other Important Scientific Packages

  • cython, numba, scikit-learn, scikit-image, h5py, pytables, Numexpr, mayavi, sphinx

Development Tools

  • spyder, Eclipse+PyDev, sphinx, pyqt, chaco, traits, VTK, pyopengl, vispy, pygame

Even More Scientific Packages

Installing Python

  • Distributions (recommended):
  • Running a remote IPython notebook in the browser:
  • Maintain your own Python installation
    • System's package manager, Linux: apt, yum, pacman, Mac: macports
    • Python package manager pip, easy_install, relying on the PyPI: the Python Package Index
    • Package manager of the Anaconda distribution conda relying on the binstar repository
    • Multiple independent virtual environments

Python 2 or 3?

  • Python 3 introduced in December 2008
  • Python 2 and Python 3 are not compatible
  • Many scientific packages support Python 3 today, but not all of them yet
  • If you still dependent on Python 2 packeges, do not use Python 3...yet
  • New Python development only for 3, it is the future!
  • Some important differences:

    • integer division in Python 2: 11/2 = 5
    • integer division in Python 3: 11/2 = 5.5
    • strings are ascii in Python 2
    • strings are unicode in Python 3
    • print statements:
      print "heho"
      print("heho")
      
  • Focus on Python versions 2.7+ and/or 3.3+

Make Python 2 Scripts Python 3 Compatible

from __future__ import division, print_function, absolute_import, unicode_literals
try:
    range = xrange
    xrange = None
except NameError: 
    pass

Further reading:


In [1]:
print len('øø')
print 4/5


4
0

In [2]:
from __future__ import division, print_function, absolute_import, unicode_literals
print(len('øø'))
print(4/5)


2
0.8

The End of the Beginning

Some of these slides have been based on the scientific Python lectures made by J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/